package com.sky.Task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

import static com.sky.entity.Orders.*;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    private OrderMapper orderMapper;
//    每分钟检查一次有没有超时订单
    @Scheduled(cron = "0 * * * * ?")
    public void processTimeOrder(){
        List<Orders>  orders=orderMapper.getTimeoutOrder(PENDING_PAYMENT, LocalDateTime.now().minusMinutes(15));
        if(!orders.isEmpty()){
            orders.forEach(order->{
                order.setStatus(CANCELLED);
                order.setCancelReason("超时了喵~");
                order.setCancelTime(LocalDateTime.now());
                orderMapper.update(order);
            });
        }

    }
    @Scheduled(cron = "0 0 1 * * ?")
    public void cleanOrder(){

        List<Orders>  orders=orderMapper.getTimeoutOrder(DELIVERY_IN_PROGRESS, LocalDateTime.now().minusHours(1));
        if(!orders.isEmpty()){
            orders.forEach(order->{
                order.setStatus(COMPLETED);
                orderMapper.update(order);
            });
        }
    }
}
